package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caifu.mapper.YlBedMapper;
import com.caifu.mapper.YlBuildingMapper;
import com.caifu.mapper.YlFloorMapper;
import com.caifu.pojo.SysUser;
import com.caifu.pojo.YlBuilding;
import com.caifu.service.IBuildingService;
import com.caifu.util.Constance;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述：楼宇实现类
 *
 * @Author GZ
 * @Date 2020/11/6 10:27
 */
@Service
public class BuildingServiceImpl implements IBuildingService {

    @Resource
    private YlBuildingMapper buildingMapper;
    @Resource
    private YlFloorMapper floorMapper;
    @Resource
    private YlBedMapper bedMapper;

    @Override
    public JSONObject saveBuildingInfo(YlBuilding building
            , SysUser user) throws Exception {
        JSONObject result = new JSONObject();
        if (null == building) {
            result.put("code", "1001");
            result.put("codeDesc", "添加失败");
            return result;
        }
        if (null == user) {
            result.put("code", "1001");
            result.put("codeDesc", "管理员为空");
            return result;
        }
        try {
            //查询名称是否存在
            YlBuilding oldBuilding = buildingMapper
                    .queryBuildingInfoByName(building.getBuildingName(),user.getCurrentPensionNo());
            if (null != oldBuilding) {
                result.put("code", "1002");
                result.put("codeDesc", "该楼苑名称已存在");
                return result;
            }
            building.setBuildingState("1"); //状态为正常状态
            building.setCreateTime(Constance.currentDateTime());
            building.setTellerNo(user.getUserNo() + "");
            building.setPensionNo(user.getCurrentPensionNo());
            buildingMapper.insert(building);
            result.put("code", "0000");
            result.put("codeDesc", "添加成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "9999");
            result.put("codeDesc", "添失败");
            return result;
        }
    }

    @Override
    public JSONObject pagedQueryBuildingInfo(SysUser user) throws Exception {
        JSONObject obj = new JSONObject();
        try {
            if (null == user) {
                obj.put("totalRecord", 0);
                return obj;
            }
            if (StringUtils.isBlank(user.getCurrentPensionNo())) {
                obj.put("totalRecord", 0);
                return obj;
            }
            String pensionNo = user.getCurrentPensionNo();
            QueryWrapper<YlBuilding> buildingQueryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(pensionNo)) {
                buildingQueryWrapper.eq("PENSION_NO", pensionNo);
            }
            buildingQueryWrapper.eq("BUILDING_STATE", "1");
            buildingQueryWrapper.orderByDesc("BUILDING_NO");
            buildingQueryWrapper.orderByDesc("CREATE_TIME");
            List<YlBuilding> buildingList = buildingMapper
                    .selectList(buildingQueryWrapper);
            int totalRecord = 0;
            if (null != buildingList && buildingList.size() > 0) {
                totalRecord = buildingList.size();
                for (YlBuilding b : buildingList
                ) {
                    if (null != b) {
                        //查询楼层数
                        int floorNum = floorMapper
                                .queryTotalFloorCounts(b.getBuildingNo() + "");
                        b.setTotalFloor(floorNum);
                        //查询总床位数
                        int totalBed = bedMapper.queryBedCountByBuildingNo(b.getBuildingNo() + "", null);
                        b.setTotalBed(totalBed);
                        //查询空床位数
                        //查询总床位数
                        int emptyBed = bedMapper.queryBedCountByBuildingNo(b.getBuildingNo() + "", "0");
                        b.setEmptyBed(emptyBed);
                    }
                }
            }
            obj.put("totalRecord", totalRecord);
            obj.put("buildingInfo", buildingList);
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            obj.put("totalRecord", 0);
            return obj;
        }
    }

    @Override
    public JSONObject queryBuildingDetail(String buildingNo) throws Exception {
        JSONObject result = new JSONObject();
        if (StringUtils.isBlank(buildingNo)) {
            result.put("code", "1001");
            result.put("codeDesc", "楼苑信息不存在");
            return result;
        }
        try {
            //查询楼苑信息
            QueryWrapper<YlBuilding> buildingQueryWrapper = new QueryWrapper<>();
            buildingQueryWrapper.eq("BUILDING_NO", buildingNo);
            YlBuilding building = buildingMapper.selectOne(buildingQueryWrapper);
            if (null == building) {
                result.put("code", "1001");
                result.put("codeDesc", "楼苑信息不存在");
                return result;
            }
            result.put("code", "0000");
            result.put("name", building.getBuildingName());
            result.put("use", building.getBuildingUse());
            result.put("img", building.getBuildingImg());
            result.put("buildingNo", building.getBuildingNo());
            result.put("codeDesc", "查询成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "1001");
            result.put("codeDesc", "楼苑信息不存在");
            return result;
        }
    }

    @Override
    public JSONObject saveBuildingUpdateInfo(YlBuilding building) throws Exception {
        JSONObject result = new JSONObject();
        if (null == building) {
            result.put("code", "1001");
            result.put("codeDesc", "要修改的信息不存在");
            return result;
        }
        try {
            //查询楼苑信息
            QueryWrapper<YlBuilding> buildingQueryWrapper = new QueryWrapper<>();
            buildingQueryWrapper.eq("BUILDING_NO", building.getBuildingNo());
            YlBuilding oldBuilding = buildingMapper.selectOne(buildingQueryWrapper);
            if (null == oldBuilding) {
                result.put("code", "1001");
                result.put("codeDesc", "楼苑信息不存在");
                return result;
            }
            String oldName = Constance.Null2EmptyTrim(oldBuilding.getBuildingName());
            String newName = Constance.Null2EmptyTrim(building.getBuildingName());
            if (!oldName.equals(newName)) {
                //查询名称是否存在
                YlBuilding otherBuilding = buildingMapper
                        .queryBuildingInfoByName(newName, oldBuilding.getPensionNo());
                if (null != otherBuilding) {
                    result.put("code", "1003");
                    result.put("codeDesc", "该楼苑名称已存在");
                    return result;
                }
            }
            if (!"".equals(Constance.Null2EmptyTrim(building.getBuildingImg()))) {
                oldBuilding.setBuildingImg(building.getBuildingImg());
            }
            oldBuilding.setBuildingName(building.getBuildingName());
            oldBuilding.setBuildingUse(building.getBuildingUse());
            buildingMapper.updateById(oldBuilding);
            result.put("code", "0000");
            result.put("codeDesc", "修改成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "9998");
            result.put("codeDesc", "修改失败");
            return result;
        }
    }

    @Override
    public JSONObject deleteBuildingInfo(String buildingNo) throws Exception {
        JSONObject result = new JSONObject();
        try {
            //查询楼苑信息
            QueryWrapper<YlBuilding> buildingQueryWrapper = new QueryWrapper<>();
            buildingQueryWrapper.eq("BUILDING_NO", buildingNo);
            YlBuilding oldBuilding = buildingMapper.selectOne(buildingQueryWrapper);
            if (null == oldBuilding) {
                result.put("code", "1001");
                result.put("codeDesc", "楼苑信息不存在");
                return result;
            }
            //查询楼苑下楼层数量
            int floorCount = floorMapper.queryTotalFloorCounts(buildingNo);
            if (floorCount > 0) {
                result.put("code", "1001");
                result.put("codeDesc", "该楼苑下已有楼层，请先删除楼层！");
                return result;
            }
            oldBuilding.setBuildingState("9");
            buildingMapper.updateById(oldBuilding);
            result.put("code", "0000");
            result.put("codeDesc", "删除成功！");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code", "9999");
            result.put("codeDesc", "删除失败");
            return result;
        }
    }

    @Override
    public List<YlBuilding> getBuildingList(SysUser sysUser) {
        QueryWrapper<YlBuilding> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PENSION_NO", sysUser.getCurrentPensionNo())
                .eq("BUILDING_STATE",1)
                .orderByAsc("BUILDING_NO")
                .orderByAsc("CREATE_TIME");
        return buildingMapper.selectList(queryWrapper);
    }

    @Override
    public List<YlBuilding> queryBuildingInfo(String pensionNo) throws Exception {
        try {
            QueryWrapper<YlBuilding> buildingQueryWrapper = new QueryWrapper<>();
            buildingQueryWrapper.eq("BUILDING_STATE","1");
            if(StringUtils.isNotBlank(pensionNo)){
                buildingQueryWrapper.eq("PENSION_NO",pensionNo);
            }
            return buildingMapper.selectList(buildingQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
